﻿/// <reference path="StoryBoard.js" />

var ajaxParam = new AjaxParam();

(function ($) {

    jQuery.extend({
        AjaxCentreSuccess: function (data, textStatus, XMLHttpRequest, target) {


            var clear = east_container;

            var doClear = true;
            if (target) {

                if (typeof (target) == "string") {
                    target = $(target);
                }


                var ajaxResponse = new jQuery.AjaxResponse(XMLHttpRequest);

                if (ajaxResponse.IsContentHtml) {

                    target.html(ajaxResponse.Content);
                    ShowTab();
                    ClearTab();
                    myLayout.close('east');


                }

                else if (ajaxResponse.IsContentJS) {

                    if (ajaxResponse.Content.indexOf('LogOut') > -1) {
                        LogOut();
                    }



                }
                else if (ajaxResponse.IsContentJSON) {
                    myLayout.open('south');
                    doClear = false;
                    try {

                        var result = jQuery.parseJSON(XMLHttpRequest.responseText);

                        var message = $("<div class='message'></div>").html('Notification: ' + result.Message);
                        $(div_error).show();
                        $(div_error_span).html(message);

                    }
                    catch (ex) {

                        var message = $("<div class='message'></div>").html(ex.ToString());
                        $(div_error_span).html(message);
                    }
                }

                if (clear && doClear) {

                    if (typeof (clear) == "string") {
                        clear = $(clear);
                    }

                    clear.html('');
                    if (!myLayout.state.east.isClosed) {
                        myLayout.close('east');
                    }
                }
            }
        }
    });




    jQuery.extend({
        StoryboardOnSuccess: function (data, textStatus, XMLHttpRequest, target) {

            var clear = east_container;

            var doClear = true;
            if (target) {

                if (typeof (target) == "string") {
                    target = $(target);
                }


                var ajaxResponse = new jQuery.AjaxResponse(XMLHttpRequest);

                if (ajaxResponse.IsContentHtml) {

                    target.html(ajaxResponse.Content);
                    groupArticle();
                    myLayout.close('east');

                }

                else if (ajaxResponse.IsContentJS) {

                    if (ajaxResponse.Content.indexOf('LogOut') > -1) {
                        LogOut();
                    }



                }
                else if (ajaxResponse.IsContentJSON) {
                    myLayout.open('south');
                    doClear = false;
                    try {

                        var result = jQuery.parseJSON(XMLHttpRequest.responseText);

                        var message = $("<div class='message'></div>").html('Notification: ' + result.Message);
                        $(div_error).show();
                        $(div_error_span).html(message);

                    }
                    catch (ex) {

                        var message = $("<div class='message'></div>").html(ex.ToString());
                        $(div_error_span).html(message);
                    }
                }

                if (clear && doClear) {

                    if (typeof (clear) == "string") {
                        clear = $(clear);
                    }

                    clear.html('');                    
                    myLayout.close('east');
                    
                }
            }
        }
    });


})(jQuery);


// if user click on user,company, or publication screen
function ClearTab()
{
    if (GetTabIndex('Publication') -1) {

        var lenght = $('#tabs').tabs('length');
        if (lenght > 1) {

            var count;
            for (var i = 1; i < lenght; i++) {

                $('#tabs').tabs('remove', 1);
                 count = $('#tabs').tabs('length');
                 if (count<2) {
                     break;
                }
            }

        }
     }
 }

 function ClearTabOnPublicationRowClick() 
 {
     var lenght = $('#tabs').tabs('length');
     if (lenght > 1) {

         var count;
         for (var i = 1; i < lenght; i++) {

             $('#tabs').tabs('remove', 1);
             count = $('#tabs').tabs('length');
             if (count < 2) {
                 break;
             }
         }

     }
 }




 function AjaxParam() 
 {
     this.target = '';
     this.tabIndexToActivate = 0;
     this.action = '';

 }